package com.csaba.swing.action;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import com.csaba.connector.model.Session;
import com.csaba.swing.MainWindow;
import com.csaba.swing.gui.ContextListener;
import com.csaba.swing.gui.GUIContext;
import com.csaba.swing.gui.SessionListView;
import com.csaba.swing.gui.util.GUIUtil;

@SuppressWarnings( "serial" )
public class SessionListAction extends AbstractAction implements ContextListener
{
	public SessionListAction()
	{
		super(MainWindow.getString("SessionListAction.menuItem")); //$NON-NLS-1$
		setEnabled(false);
		putValue(SMALL_ICON, GUIContext.getIcon("profile", 16)); //$NON-NLS-1$
		putValue(LARGE_ICON_KEY, GUIContext.getIcon("profile", 32)); //$NON-NLS-1$
		GUIUtil.setMnemonic(this);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0));
		GUIContext.getInstance().addContextListener(this);
	}

	@Override
	public void actionPerformed( final ActionEvent arg0 )
	{
		final GUIContext context = GUIContext.getInstance();
		if ( !context.hasActiveSession() )
		{
			JOptionPane
					.showMessageDialog(
							MainWindow.getFrame(),
							MainWindow.getString("SessionListAction.msg.noSession"), MainWindow.getString("SessionListAction.titleError"), //$NON-NLS-1$ //$NON-NLS-2$
							JOptionPane.ERROR_MESSAGE);
			return;
		}
		final Session[] sessions = context.getSessions().toArray(new Session[context.getSessions().size()]);

		final SessionListView view = new SessionListView();
		MainWindow.getDesktop().add(view);
		view.showIt(sessions);
	}

	@Override
	public void contextEventPerformed( final int eventType )
	{
		setEnabled(GUIContext.getInstance().hasActiveSession());
	}

}
